const Router = require('koa-router');
const AuthService = require('../services/authService');
const { authenticateToken, authorize } = require('../middlewares/auth.middleware');

const router = new Router();

/**
 * 认证相关路由
 */

// 用户注册
router.post('/register', async (ctx) => {
  try {
    const { username, email, password, phone } = ctx.request.body;
    
    const result = await AuthService.register({
      username,
      email,
      password,
      phone
    });
    
    ctx.status = 201;
    ctx.body = {
      success: true,
      message: '注册成功',
      data: result
    };
  } catch (error) {
    ctx.status = error.status || 400;
    ctx.body = {
      success: false,
      message: error.message
    };
  }
});

// 用户登录
router.post('/login', async (ctx) => {
  try {
    const { username, password, captchaKey, captcha } = ctx.request.body;
    
    // 调用 AuthService.login，传入 username 和 password
    const result = await AuthService.login(username, password);
    
    ctx.status = 200;
    ctx.body = {
      success: true,
      message: '登录成功',
      data: {
        token: result.tokens.accessToken,
        refreshToken: result.tokens.refreshToken,
        role: result.user.role,
        name: result.user.name,
        email: result.user.email,
        username: result.user.username
      }
    };
  } catch (error) {
    ctx.status = error.status || 401;
    ctx.body = {
      success: false,
      message: error.message
    };
  }
});

// 刷新访问令牌
router.post('/refresh-token', async (ctx) => {
  try {
    const { refreshToken } = ctx.request.body;
    
    const result = await AuthService.refreshToken(refreshToken);
    
    ctx.status = 200;
    ctx.body = {
      success: true,
      message: '令牌刷新成功',
      data: result
    };
  } catch (error) {
    ctx.status = error.status || 401;
    ctx.body = {
      success: false,
      message: error.message
    };
  }
});

// 登出
router.post('/logout', authenticateToken, async (ctx) => {
  try {
    const userId = ctx.state.userId;
    
    await AuthService.logout(userId);
    
    ctx.status = 200;
    ctx.body = {
      success: true,
      message: '登出成功'
    };
  } catch (error) {
    ctx.status = error.status || 500;
    ctx.body = {
      success: false,
      message: error.message
    };
  }
});

// 生成验证码
router.get('/captcha', async (ctx) => {
  try {
    const result = await AuthService.generateCaptcha();
    
    ctx.status = 200;
    ctx.body = {
      success: true,
      data: result
    };
  } catch (error) {
    ctx.status = error.status || 500;
    ctx.body = {
      success: false,
      message: error.message
    };
  }
});

// 验证令牌是否有效
router.get('/verify-token', authenticateToken, async (ctx) => {
  try {
    const user = ctx.state.user;
    
    ctx.status = 200;
    ctx.body = {
      success: true,
      message: '令牌有效',
      data: {
        userId: user._id,
        username: user.username,
        email: user.email,
        role: user.role
      }
    };
  } catch (error) {
    ctx.status = 401;
    ctx.body = {
      success: false,
      message: '令牌无效'
    };
  }
});

module.exports = router;